Skip to content

refactor: 拆分三个单体大文件(DiffSearchPanel / DraftZone / ChatInputBar)#84

Merged
huhamhire merged 3 commits into
devfrom
refactor/split-monolith-files
Jun 19, 2026
Merged

refactor: 拆分三个单体大文件(DiffSearchPanel / DraftZone / ChatInputBar)#84
huhamhire merged 3 commits into
devfrom
refactor/split-monolith-files

Conversation

@huhamhire

Copy link
Copy Markdown
Owner

背景

继 DiffView(#82)/ common barrel(#83)之后,renderer 下仍有三个职责混杂的单体组件(复杂状态机 / 纯算法 + 渲染糅在一处)。本 PR 按「纯算法 → util;状态机 → hook;渲染 → 瘦组件」拆分,运行时行为零变化(纯结构搬运;依赖数组 / 事件时序 / eslint-disable 兜底原样保留)。

三笔独立 commit:

1. refactor(diff) DiffSearchPanel 576 → ~190 行

  • diff/search/diff-search.ts:纯跨文件搜索逻辑(runSearch + findChangedIndices / expandToContext / stripLeadingIndent / findMatchSpan / countLines / loadContent)+ 类型 / 常量,无 React 依赖
  • diff/search/colorize.ts:colorizeAll(Monaco editor.colorize 异步语法着色)
  • diff/search/useDiffSearch.ts:搜索状态机 hook(query / 大小写持久化 / 结果 / loading / 折叠态 + 去抖 + 内容缓存 + 自动聚焦)

2. refactor(drafts) DraftZone 531 → ~230 行

  • drafts/useDraftZone.ts:read/edit/publish 状态机 hook(全部 state / ref / effect / handler;取消四档逻辑 runCancelLogic 被取消按钮 / unmount cleanup / Esc 共用,整体搬进 hook 保持行为一致)

3. refactor(chat) ChatInputBar 479 → ~240 行

  • chat/utils/parse-command.ts:纯命令解析 parseChatCommand → 判别式结果,无 i18n / 副作用
  • chat/hooks/useChatInput.ts:输入栏状态机(输入 / 提交 / 自动补全 / 历史回放 / 停止请求)
  • chat/hooks/useTextareaAutosizeDrag.ts:textarea 顶边拖拽调高

行为保留 & 外部面

三个文件的公共导出名与路径均不变,外部引用为单点(DiffSearchPanel ← DiffView;DraftZone ← DraftZoneList;ChatInputBar ← ChatPane),零波及。

验证

lint 零警告 ✓ · typecheck ✓ · build ✓ · test(仅 packages;repo-mirror 5 个 git-CLI 环境性失败属已知、与本改动无关)。

人工回归重点:搜索(去抖 / 大小写持久化 / 着色 / 折叠 / 跳转 / Esc 退出)、草稿(read↔edit / 四档取消 / Cmd+Enter 发布 / 切文件自动保存 / 删除确认)、输入栏(命令解析与未知报错 / 自动补全 Up/Down/Enter/Tab/Esc / 历史回放 / textarea resize / 发送停止 / IME 不拦截)。

关联里程碑 代码质量重构 (#3)

🤖 Generated with Claude Code

huhamhire and others added 3 commits June 19, 2026 16:49
DiffSearchPanel.tsx 576 → ~190 行,退化为瘦渲染组件。新增 search/ 子目录:

- search/diff-search.ts:纯跨文件搜索逻辑(runSearch + findChangedIndices /
  expandToContext / stripLeadingIndent / findMatchSpan / countLines / loadContent)
  + 类型 LineMatch / FileResults + 常量,无 React 依赖
- search/colorize.ts:colorizeAll(Monaco editor.colorize 异步语法着色)
- search/useDiffSearch.ts:搜索状态机 hook(query / 大小写持久化 / 结果 /
  loading / 折叠态 + 去抖搜索 + 内容缓存 + 自动聚焦)

纯结构拆分,行为不变;DiffSearchPanel 导出名与路径保持不变(DiffView 引用零波及)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
DraftZone.tsx 531 → ~230 行,退化为瘦渲染组件。

- useDraftZone.ts:read/edit/publish 状态机 hook —— 全部 state(isEditing /
  editingBody / saving / publishing / publishError / confirmDelete)+ ref(textarea +
  4 个同步 ref + isMutating 锁)+ effect(ref 同步 / mutate 锁 / unmount cleanup /
  registerEditTrigger / draft.body 同步 / focus / Esc window listener)+ handler
  (runCancelLogic / handleSave / handlePublish / handlePublishFromEdit / handleCancel /
  handleDelete / handleConfirmDelete / onKeyDown)+ 派生(canEdit / canSave / isStash)
- DraftZone.tsx:消费 hook 做渲染(head / edit / read / foot / 错误 / ConfirmModal)

取消四档逻辑(取消按钮 / unmount cleanup / Esc 共用 runCancelLogic 读 ref 最新值)整体
搬进 hook,行为不变;eslint-disable 兜底原样保留。DraftZone 导出名与路径不变。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
ChatInputBar.tsx 479 → ~240 行,退化为瘦渲染组件。

- utils/parse-command.ts:纯命令解析 parseChatCommand → 判别式结果
  (unknown / commandNoArgs / askNeedsQuestion / reviewAction / run / agentAsk),无 i18n / 副作用
- hooks/useChatInput.ts:输入栏状态机 hook —— 输入 / 提交(调 parseChatCommand 映射回调)/
  自动补全浮层 / 历史回放(shell 式 Up/Down)/ 停止请求 + 相关 effect
- hooks/useTextareaAutosizeDrag.ts:textarea 顶边拖拽调高
- ChatInputBar.tsx:消费两个 hook 做渲染(补全列表 / textarea / 命令栏 / 自动评审 / 发送停止)

纯结构拆分,行为不变;ChatInputBar 导出名与路径不变(ChatPane 引用零波及)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@huhamhire huhamhire added this to the 代码质量重构 milestone Jun 19, 2026
@huhamhire huhamhire added the refactor 代码重构(不改运行行为) label Jun 19, 2026
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@huhamhire huhamhire merged commit 5493ef9 into dev Jun 19, 2026
1 check passed
@huhamhire huhamhire deleted the refactor/split-monolith-files branch June 19, 2026 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor 代码重构(不改运行行为)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant